package com.jzh.selfwallpaper.view.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Observable;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jzh.selfwallpaper.R;
import com.jzh.selfwallpaper.model.Model;
import com.jzh.selfwallpaper.util.DateUtil;
import com.jzh.selfwallpaper.util.ImageUtil;
import com.jzh.selfwallpaper.util.PhoneUtil;
import com.jzh.selfwallpaper.util.ScreenShootUtil;
import com.jzh.selfwallpaper.view.listener.OnDoubleClickListener;

import imageloader.libin.com.images.loader.ImageLoader;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.jzh.selfwallpaper.constant.Constant.IMAGE;
import static com.jzh.selfwallpaper.util.DateUtil.DF_YYYY_MM_DD_HH_MM_SS_WAV;

public class Model_1 extends RelativeLayout implements View.OnClickListener, View.OnTouchListener,Model {

    //缩略比例
    private float thumbnail = 0.5f;
    private int base_blur = 30;
    private String current_url = null;
    private View viewModel;
    private Context context;
    private ImageView iv_model1_bg, iv_model1_photo, iv_model1_photo_bg;
    private TextView mtv_first_name, mtv_say;
    private RelativeLayout rl_wallpaper;
    private OnModel1WidgetClick onModel1WidgetClick;


    public Model_1(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public Model_1(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public void setOnModel1WidgetClick(OnModel1WidgetClick onModel1WidgetClick) {
        this.onModel1WidgetClick = onModel1WidgetClick;
    }

    float mPosX = 0, mPosY, mCurPosX = 0, mCurPosY = 0;
    private int count = 0;//点击次数
    private long firstClick = 0;//第一次点击时间
    private long secondClick = 0;//第二次点击时间
    /**
     * 两次点击时间间隔，单位毫秒
     */
    private final int totalTime = 500;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPosX = event.getX();
                mPosY = event.getY();
                //处理双击代码块
            {
                count++;
                if (1 == count) {
                    firstClick = System.currentTimeMillis();//记录第一次点击时间
                } else if (2 == count) {
                    secondClick = System.currentTimeMillis();//记录第二次点击时间
                    if (secondClick - firstClick < totalTime) {//判断二次点击时间间隔是否在设定的间隔时间之内
                        onDoubleClick();
                        count = 0;
                        firstClick = 0;
                    } else {
                        firstClick = secondClick;
                        count = 1;
                    }
                    secondClick = 0;
                }
            }
            break;
            case MotionEvent.ACTION_MOVE:
                mCurPosX = event.getX();
                mCurPosY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                int length = (int) Math.abs(mCurPosX - mPosX);
                if (mCurPosX - mPosX > 0 && (length > 25)) {
                    //向左滑動
                    base_blur = base_blur - length / 30;
                    if (base_blur > 0 && base_blur < 200) {
                        //设置背景图片
                        if (current_url != null) {
                            ImageLoader.with(context)
                                    .url(current_url)
                                    .blur(base_blur)
                                    .into(iv_model1_bg);
                        } else {
                            ImageLoader.with(context)
                                    .res(R.drawable.zrn)
                                    .blur(base_blur)
                                    .into(iv_model1_bg);
                        }
                    } else {
                        base_blur = base_blur + length / 30;
                        Toast.makeText(context, "模糊效果超出范围", Toast.LENGTH_SHORT).show();
                    }
                } else if (mCurPosX - mPosX < 0 && (length > 25)) {
                    //向右滑动
                    base_blur = base_blur + length / 20;
                    if (base_blur > 0 && base_blur < 200) {
                        //设置背景图片
                        if (current_url != null) {
                            ImageLoader.with(context)
                                    .url(current_url)
                                    .blur(base_blur)
                                    .into(iv_model1_bg);
                        } else {
                            ImageLoader.with(context)
                                    .res(R.drawable.zrn)
                                    .blur(base_blur)
                                    .into(iv_model1_bg);
                        }
                    } else {
                        base_blur = base_blur - length / 30;
                        Toast.makeText(context, "模糊效果超出范围", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
        return true;
    }

    private void onDoubleClick() {
        rx.Observable.create(new rx.Observable.OnSubscribe<Bitmap>() {
            @Override
            public void call(Subscriber<? super Bitmap> subscriber) {
                Bitmap bitmap = ScreenShootUtil.getScreenShoot2(rl_wallpaper);
                ImageUtil.saveBitmapToSDCard(bitmap,DateUtil.getCurrentDateStr(DF_YYYY_MM_DD_HH_MM_SS_WAV));
                subscriber.onNext(bitmap);
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Bitmap>() {
                    @Override
                    public void onCompleted() {
                        Toast.makeText(context, "壁纸保存成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(context, "壁纸保存失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(Bitmap bitmap) {
                    }
                });
    }

    public interface OnModel1WidgetClick {
        void onModel1PhotoClick();
    }

    private void init() {

        viewModel = LayoutInflater.from(context).inflate(R.layout.model_1, this, true);
        rl_wallpaper=viewModel.findViewById(R.id.rl_wallpaper);
        iv_model1_bg = viewModel.findViewById(R.id.iv_model1_bg);
        iv_model1_bg.setOnTouchListener(this);
        iv_model1_photo = viewModel.findViewById(R.id.iv_model1_photo);
        iv_model1_photo.setOnClickListener(this);
        iv_model1_photo_bg = viewModel.findViewById(R.id.iv_model1_photo_bg);
        mtv_first_name = viewModel.findViewById(R.id.mtv_first_name);
        mtv_say = viewModel.findViewById(R.id.mtv_say);
        //设置背景图片
        ImageLoader.with(context)
                .res(R.drawable.zrn)
                .blur(base_blur)
                .into(iv_model1_bg);

        ImageLoader.with(context)
                .res(R.drawable.zrn)
                .asCircle()
                .into(iv_model1_photo);

        ImageLoader.with(context)
                .asCircle()
                .into(iv_model1_photo_bg);

        mtv_first_name.setText("章");
        setTypeface(mtv_first_name, 1);
        mtv_say.setText("我姓\n却彰显不出我的个性");
        setTypeface(mtv_first_name, 2);
    }

    private void setTypeface(TextView tv, int kind) {
        Typeface fontFace = null;
        switch (kind) {
            case 0:
                fontFace = Typeface.createFromAsset(context.getAssets(), "WallauUnzial.ttf");
                break;
            case 1:
                fontFace = Typeface.createFromAsset(context.getAssets(), "CornDog.ttf");
                break;
        }
        tv.setTypeface(fontFace);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_model1_photo:
                PhoneUtil.openAlbum(context);
                break;
        }
    }

    public void setPhoto(String path) {
        if (path == null || path.equals("")) {
            Toast.makeText(context, "图片加载错误！", Toast.LENGTH_SHORT).show();
            return;
        }
        current_url = path;
        ImageLoader.with(context)
                .url(current_url)
                .asCircle()
                .into(iv_model1_photo);
        ImageLoader.with(context)
                .url(current_url)
                .blur(base_blur)
                .into(iv_model1_bg);
    }
}
